package com.magus.project.yddemo.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.annotation.JsonView;
import com.magus.framework.core.controller.BaseController;
import com.magus.framework.core.entity.BaseTreeNode.WithOutTreeView;
import com.magus.framework.system.annotation.AppFunctionInfo;
import com.magus.framework.system.annotation.AppFunctionInfo.FunctionType;
import com.magus.framework.system.annotation.AppGroupInfo;
import com.magus.project.yddemo.entity.Indicator;
import com.magus.project.yddemo.entity.IndicatorGroup;
import com.magus.project.yddemo.service.IndicatorGroupService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 指标组树结构管理
 * 
 * @author xujuan
 * @date 2018-01-19 15:26:53
 */
@Api(tags = "IndicatorGroupManager", description = "指标组管理")
@RestController
@RequestMapping("/yddemo/indicatorGroups")
@AppGroupInfo(key = "yddemo:indicatorGroup", name = "指标组管理", appKey = "yddemo")
public class IndicatorGroupController extends BaseController {

	@Autowired
	private IndicatorGroupService groupService;

	/**
	 * 根据ID查询指标组
	 * 
	 * @param id
	 *            指标组ID
	 * @return
	 */
	@JsonView(WithOutTreeView.class)
	@GetMapping("/{id}")
	@RequiresPermissions("yddemo:indicatorGroup:view")
	@ApiOperation(value = "查询指标组", notes = "根据ID查询指标组")
	ResponseEntity<IndicatorGroup> findOne(@PathVariable String id) {
		IndicatorGroup group = groupService.findOne(id);
		if (group == null) {
			return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
		}
		return ResponseEntity.ok(group);
	}

	@GetMapping
	@RequiresPermissions("yddemo:indicatorGroup:view")
	@ApiOperation(value = "查询指标", notes = "查询此指标组下面的指标")
	@AppFunctionInfo(key = "yddemo:indicatorGroup:view", name = "查询指标列表", groupKey = "yddemo:indicatorGroup", //
			isMenu = false, type = FunctionType.PAGE, url = "/yddemo/indicatorGroups/page/view")
	ResponseEntity<List<Indicator>> findAll(@RequestParam(required = false) String groupId) {

		return null;
	}

	/**
	 * 新增指标组
	 * 
	 * @param group
	 * @param result
	 * @return
	 */
	@JsonView(WithOutTreeView.class)
	@PostMapping
	@RequiresPermissions("yddemo:indicatorGroup:add")
	@ApiOperation(value = "新增指标组", notes = "返回成功保存后的对象")
	@AppFunctionInfo(key = "yddemo:indicatorGroup:add", name = "新增指标组", groupKey = "yddemo:indicator", //
			isMenu = false, type = FunctionType.PAGE, url = "/yddemo/indicatorGroups/page/add")
	ResponseEntity<IndicatorGroup> add(@RequestBody IndicatorGroup group, BindingResult result) {
		validate(group, result);
		IndicatorGroup saved = groupService.save(group);
		return ResponseEntity.status(HttpStatus.CREATED).body(saved);
	}

	/**
	 * 编辑指标组
	 * 
	 * @param id
	 * @param group
	 * @param result
	 * @return
	 */
	@JsonView(WithOutTreeView.class)
	@PutMapping("/{id}")
	@RequiresPermissions("yddemo:indicatorGroup:edit")
	@ApiOperation(value = "编辑指标组", notes = "返回成功保存后的对象")
	@AppFunctionInfo(key = "yddemo:indicatorGroup:edit", name = "编辑指标组", groupKey = "yddemo:indicatorGroup", //
			isMenu = false, type = FunctionType.PAGE, url = "/yddemo/indicatorGroups/page/edit")
	ResponseEntity<IndicatorGroup> update(@PathVariable String id, @RequestBody IndicatorGroup group,
			BindingResult result) {
		IndicatorGroup old = groupService.findOne(id);
		if (old == null) {
			return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
		}
		group.setId(old.getId());

		validate(group, result);
		IndicatorGroup saved = groupService.save(group);
		return ResponseEntity.ok(saved);
	}

	/**
	 * 删除指标组
	 * 
	 * @param id
	 * @return
	 */
	@DeleteMapping("/{id}")
	@RequiresPermissions("yddemo:indicatorGroup:delete")
	@ApiOperation(value = "删除指标组", notes = "根据ID删除指标组")
	@AppFunctionInfo(key = "yddemo:indicatorGroup:delete", name = "删除指标组", groupKey = "yddemo:indicatorGroup", //
			isMenu = false, type = FunctionType.BUTTON, url = "/yddemo/indicatorGroups")
	ResponseEntity<Void> delete(@PathVariable String id) {
		IndicatorGroup old = groupService.findOne(id);
		if (old == null) {
			return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
		}
		// 不删除
		// groupService.delete(old);
		return ResponseEntity.noContent().build();
	}

}
